{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import lightgbm as lgb\n",
    "\n",
    "train_features = np.load('./data/train_features.npy')\n",
    "train_labels = np.load('./data/train_labels.npy')\n",
    "test_features = np.load('./data/test_features.npy')\n",
    "\n",
    "data_train = lgb.Dataset(train_features, label = train_labels)\n",
    "\n",
    "num_inputs = 21\n",
    "num_outputs = 39\n",
    "\n",
    "params = {\n",
    "    'boosting': 'gbdt', \n",
    "    'objective': 'multiclass',\n",
    "    'metrics' : 'multi_logloss',\n",
    "    'num_class': num_outputs,\n",
    "    'verbosity': 1,                   #以上不再调整\n",
    "    'max_depth': 6,\n",
    "    'num_leaves': 50,                 #常用数值，备调，step2\n",
    "    'min_data_in_leaf' : 20,          #默认数值，备调，step3\n",
    "    'feature_fraction': 0.8,          #常用数值，备调，step4\n",
    "    'learning_rate': 0.1,             #默认数值，备调，step5\n",
    "    }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Step1. num_boost_round"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20]\tcv_agg's multi_logloss: 2.50468 + 0.031806\n",
      "[40]\tcv_agg's multi_logloss: 2.43431 + 0.00721752\n",
      "[60]\tcv_agg's multi_logloss: 2.42023 + 0.00977275\n",
      "[80]\tcv_agg's multi_logloss: 2.4096 + 0.00525619\n",
      "[100]\tcv_agg's multi_logloss: 2.39575 + 0.00471342\n",
      "[120]\tcv_agg's multi_logloss: 2.39878 + 0.0136678\n",
      "[140]\tcv_agg's multi_logloss: 2.39512 + 0.0103291\n",
      "[160]\tcv_agg's multi_logloss: 2.38855 + 0.00680853\n",
      "[180]\tcv_agg's multi_logloss: 2.39569 + 0.0110776\n",
      "[200]\tcv_agg's multi_logloss: 2.3862 + 0.00131608\n",
      "[220]\tcv_agg's multi_logloss: 2.39397 + 0.0182124\n",
      "[240]\tcv_agg's multi_logloss: 2.39446 + 0.0215042\n",
      "best num_boost_round: 214\n",
      "last mean: 2.379645842199627\n",
      "last stdv: 0.0024214570330422813\n"
     ]
    }
   ],
   "source": [
    "results = lgb.cv(params,\n",
    "                 data_train,\n",
    "                 num_boost_round = 1000,\n",
    "                 nfold = 3,\n",
    "                 shuffle = True,\n",
    "                 early_stopping_rounds = 40,\n",
    "                 verbose_eval = 20)\n",
    "print('best num_boost_round:', len(results['multi_logloss-mean']))\n",
    "print('last mean:', results['multi_logloss-mean'][-1])\n",
    "print('last stdv:', results['multi_logloss-stdv'][-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = {\n",
    "    'boosting': 'gbdt', \n",
    "    'objective': 'multiclass',\n",
    "    'metrics' : 'multi_logloss',\n",
    "    'num_class': num_outputs,\n",
    "    'verbosity': 1,                   #以上不再调整\n",
    "    'max_depth': 6,\n",
    "    'num_leaves': 50,                 #常用数值，备调，step2\n",
    "    'min_data_in_leaf' : 20,          #默认数值，备调，step3\n",
    "    'feature_fraction': 0.8,          #常用数值，备调，step4\n",
    "    'learning_rate': 0.1,             #默认数值，备调，step5\n",
    "    }\n",
    "gbm = lgb.train(params, data_train, num_boost_round = 214)\n",
    "gbm.save_model('../working/gbm(v1).txt')\n",
    "testResult = gbm.predict(test_features)\n",
    "sampleSubmission = pd.read_csv('../input/sf-crime/sampleSubmission.csv.zip')\n",
    "Result_pd = pd.DataFrame(testResult,\n",
    "                         index=sampleSubmission.index,\n",
    "                         columns=sampleSubmission.columns[1:])\n",
    "Result_pd.to_csv('../working/sampleSubmission(gbmv1).csv', index_label='Id')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "提交"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Step2. max_depth 和 num_leaves"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "max_depth = 3 , num_leaves = 20\n",
      "[100]\tcv_agg's multi_logloss: 2.44552 + 0.00163549\n",
      "[200]\tcv_agg's multi_logloss: 2.42769 + 0.00962192\n",
      "last mean: 2.4220655707599197\n",
      "last stdv: 0.0065027223149882645\n",
      "max_depth = 3 , num_leaves = 60\n",
      "[100]\tcv_agg's multi_logloss: 2.44552 + 0.00163549\n",
      "[200]\tcv_agg's multi_logloss: 2.42769 + 0.00962192\n",
      "last mean: 2.4220655707599197\n",
      "last stdv: 0.0065027223149882645\n",
      "max_depth = 5 , num_leaves = 20\n",
      "[100]\tcv_agg's multi_logloss: 2.40996 + 0.00366558\n",
      "[200]\tcv_agg's multi_logloss: 2.39024 + 0.00372836\n",
      "last mean: 2.3893025015374247\n",
      "last stdv: 0.003230273930546185\n",
      "max_depth = 5 , num_leaves = 60\n",
      "[100]\tcv_agg's multi_logloss: 2.41023 + 0.00888953\n",
      "[200]\tcv_agg's multi_logloss: 2.39048 + 0.00908936\n",
      "last mean: 2.387051839466166\n",
      "last stdv: 0.009956365968283616\n",
      "max_depth = 7 , num_leaves = 20\n",
      "last mean: 2.577312549960696\n",
      "last stdv: 0.016977703463318776\n",
      "max_depth = 7 , num_leaves = 60\n",
      "last mean: 2.4587145258610956\n",
      "last stdv: 0.008400881474445983\n"
     ]
    }
   ],
   "source": [
    "for max_depth in range(3,8,2):\n",
    "    for num_leaves in range(20, 70, 40):\n",
    "        params['max_depth'] = max_depth\n",
    "        params['num_leaves'] = num_leaves\n",
    "        print('max_depth = %d , num_leaves = %d' %\n",
    "              (params['max_depth'] , params['num_leaves']))\n",
    "        results = lgb.cv(params,\n",
    "                         data_train,\n",
    "                         num_boost_round = 214,\n",
    "                         nfold = 3,\n",
    "                         shuffle = True,\n",
    "                         early_stopping_rounds = 40,\n",
    "                         verbose_eval = 100)\n",
    "        print('last mean:', results['multi_logloss-mean'][-1])\n",
    "        print('last stdv:', results['multi_logloss-stdv'][-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "接着细调："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "max_depth = 6 , num_leaves = 35\n",
      "last mean: 2.4891922482617757\n",
      "last stdv: 0.010804137678622594\n",
      "max_depth = 6 , num_leaves = 45\n",
      "last mean: 2.4337760882358275\n",
      "last stdv: 0.023151302306342825\n",
      "max_depth = 6 , num_leaves = 55\n",
      "last mean: 2.4854409810898215\n",
      "last stdv: 0.0020869982223483607\n"
     ]
    }
   ],
   "source": [
    "for max_depth in (6,):\n",
    "    for num_leaves in (35, 45, 55):\n",
    "        params['max_depth'] = max_depth\n",
    "        params['num_leaves'] = num_leaves\n",
    "        print('max_depth = %d , num_leaves = %d' %\n",
    "              (params['max_depth'] , params['num_leaves']))\n",
    "        results = lgb.cv(params,\n",
    "                         data_train,\n",
    "                         num_boost_round = 214,\n",
    "                         nfold = 3,\n",
    "                         shuffle = True,\n",
    "                         early_stopping_rounds = 40,\n",
    "                         verbose_eval = 100)\n",
    "        print('last mean:', results['multi_logloss-mean'][-1])\n",
    "        print('last stdv:', results['multi_logloss-stdv'][-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "max_depth = 6 , num_leaves = 40\n",
      "[100]\tcv_agg's multi_logloss: 2.39696 + 0.0011884\n",
      "last mean: 2.3864925333355216\n",
      "last stdv: 0.004396506644347127\n",
      "max_depth = 6 , num_leaves = 50\n",
      "[100]\tcv_agg's multi_logloss: 2.39575 + 0.00471342\n",
      "[200]\tcv_agg's multi_logloss: 2.3862 + 0.00131608\n",
      "last mean: 2.379645842199627\n",
      "last stdv: 0.0024214570330422813\n",
      "max_depth = 5 , num_leaves = 40\n",
      "[100]\tcv_agg's multi_logloss: 2.41023 + 0.00888953\n",
      "[200]\tcv_agg's multi_logloss: 2.39048 + 0.00908936\n",
      "last mean: 2.387051839466166\n",
      "last stdv: 0.009956365968283616\n",
      "max_depth = 5 , num_leaves = 50\n",
      "[100]\tcv_agg's multi_logloss: 2.41023 + 0.00888953\n",
      "[200]\tcv_agg's multi_logloss: 2.39048 + 0.00908936\n",
      "last mean: 2.387051839466166\n",
      "last stdv: 0.009956365968283616\n"
     ]
    }
   ],
   "source": [
    "for max_depth in (6, 5):\n",
    "    for num_leaves in (40, 50):\n",
    "        params['max_depth'] = max_depth\n",
    "        params['num_leaves'] = num_leaves\n",
    "        #print(params)\n",
    "        #'''\n",
    "        print('max_depth = %d , num_leaves = %d' %\n",
    "              (params['max_depth'] , params['num_leaves']))\n",
    "        results = lgb.cv(params,\n",
    "                         data_train,\n",
    "                         num_boost_round = 214,\n",
    "                         nfold = 3,\n",
    "                         shuffle = True,\n",
    "                         early_stopping_rounds = 40,\n",
    "                         verbose_eval = 100)\n",
    "        print('last mean:', results['multi_logloss-mean'][-1])\n",
    "        print('last stdv:', results['multi_logloss-stdv'][-1])\n",
    "        #'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "max_depth = 6 , num_leaves = 38\n",
      "[100]\tcv_agg's multi_logloss: 2.3998 + 0.00411068\n",
      "last mean: 2.3915816256398634\n",
      "last stdv: 0.002051365366130591\n",
      "max_depth = 6 , num_leaves = 42\n",
      "[100]\tcv_agg's multi_logloss: 2.41089 + 0.00653975\n",
      "last mean: 2.3941764198379887\n",
      "last stdv: 0.005884772657223358\n",
      "max_depth = 6 , num_leaves = 48\n",
      "[100]\tcv_agg's multi_logloss: 2.40125 + 0.00905401\n",
      "[200]\tcv_agg's multi_logloss: 2.41403 + 0.0266957\n",
      "last mean: 2.388492100373433\n",
      "last stdv: 0.004900892114415105\n",
      "max_depth = 6 , num_leaves = 52\n",
      "last mean: 2.432784516485041\n",
      "last stdv: 0.007515011426455112\n"
     ]
    }
   ],
   "source": [
    "for max_depth in (6,):\n",
    "    for num_leaves in (38, 42, 48, 52):\n",
    "        params['max_depth'] = max_depth\n",
    "        params['num_leaves'] = num_leaves\n",
    "        #print(params)\n",
    "        #'''\n",
    "        print('max_depth = %d , num_leaves = %d' %\n",
    "              (params['max_depth'] , params['num_leaves']))\n",
    "        results = lgb.cv(params,\n",
    "                         data_train,\n",
    "                         num_boost_round = 214,\n",
    "                         nfold = 3,\n",
    "                         shuffle = True,\n",
    "                         early_stopping_rounds = 40,\n",
    "                         verbose_eval = 100)\n",
    "        print('last mean:', results['multi_logloss-mean'][-1])\n",
    "        print('last stdv:', results['multi_logloss-stdv'][-1])\n",
    "        #'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "max_depth = 6 , num_leaves = 49\n",
      "[100]\tcv_agg's multi_logloss: 2.45417 + 0.0735405\n",
      "last mean: 2.440617942865168\n",
      "last stdv: 0.037959388186554255\n",
      "max_depth = 6 , num_leaves = 51\n",
      "[100]\tcv_agg's multi_logloss: 2.3997 + 0.00450754\n",
      "[200]\tcv_agg's multi_logloss: 2.38764 + 0.0061293\n",
      "last mean: 2.3793165805708867\n",
      "last stdv: 0.0016149213685509095\n",
      "max_depth = 6 , num_leaves = 39\n",
      "[100]\tcv_agg's multi_logloss: 2.48038 + 0.070169\n",
      "last mean: 2.428458342356551\n",
      "last stdv: 0.01570288199424567\n",
      "max_depth = 6 , num_leaves = 41\n",
      "last mean: 2.4222211399544835\n",
      "last stdv: 0.007000683981481878\n"
     ]
    }
   ],
   "source": [
    "for max_depth in (6,):\n",
    "    for num_leaves in (49, 51, 39, 41):\n",
    "        params['max_depth'] = max_depth\n",
    "        params['num_leaves'] = num_leaves\n",
    "        #print(params)\n",
    "        #'''\n",
    "        print('max_depth = %d , num_leaves = %d' %\n",
    "              (params['max_depth'] , params['num_leaves']))\n",
    "        results = lgb.cv(params,\n",
    "                         data_train,\n",
    "                         num_boost_round = 214,\n",
    "                         nfold = 3,\n",
    "                         shuffle = True,\n",
    "                         early_stopping_rounds = 40,\n",
    "                         verbose_eval = 100)\n",
    "        print('last mean:', results['multi_logloss-mean'][-1])\n",
    "        print('last stdv:', results['multi_logloss-stdv'][-1])\n",
    "        #'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "max_depth = 5 , num_leaves = 10\n",
      "last mean: 2.4980897550813435\n",
      "last stdv: 0.0004637598353433645\n",
      "max_depth = 5 , num_leaves = 30\n",
      "[100]\tcv_agg's multi_logloss: 2.41008 + 0.0121798\n",
      "[200]\tcv_agg's multi_logloss: 2.38782 + 0.00929667\n",
      "last mean: 2.3878041620756467\n",
      "last stdv: 0.008733961375833862\n"
     ]
    }
   ],
   "source": [
    "for max_depth in (5,):\n",
    "    for num_leaves in (10, 30):\n",
    "        params['max_depth'] = max_depth\n",
    "        params['num_leaves'] = num_leaves\n",
    "        #print(params)\n",
    "        #'''\n",
    "        print('max_depth = %d , num_leaves = %d' %\n",
    "              (params['max_depth'] , params['num_leaves']))\n",
    "        results = lgb.cv(params,\n",
    "                         data_train,\n",
    "                         num_boost_round = 214,\n",
    "                         nfold = 3,\n",
    "                         shuffle = True,\n",
    "                         early_stopping_rounds = 40,\n",
    "                         verbose_eval = 100)\n",
    "        print('last mean:', results['multi_logloss-mean'][-1])\n",
    "        print('last stdv:', results['multi_logloss-stdv'][-1])\n",
    "        #'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "得到下表："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "max_depth | num_leaves | loss-mean | loss-stdv\n",
    ":-: | :-: | :-: | :-:\n",
    "3 | 20 | 2.422065570759919 | 0.00650272231498826\n",
    "3 | 60 | 2.422065570759919 | 0.00650272231498826\n",
    "5 | 10 | 2.498089755081343 | 0.00046375983534336\n",
    "5 | 20 | **2.389302501537424** | 0.00323027393054618\n",
    "5 | 30 | **2.387804162075646** | 0.00873396137583386\n",
    "5 | 40 | **2.387051839466166** | 0.00995636596828361\n",
    "5 | 50 | **2.387051839466166** | 0.00995636596828361\n",
    "5 | 60 | **2.387051839466166** | 0.00995636596828361\n",
    "6 | 35 | 2.489192248261775 | 0.01080413767862259\n",
    "6 | 38 | **2.391581625639863** | 0.00205136536613059\n",
    "6 | 39 | 2.428458342356551 | 0.01570288199424567\n",
    "6 | 40 | **2.386492533335521** | 0.00439650664434712\n",
    "6 | 41 | 2.422221139954483 | 0.00700068398148187\n",
    "6 | 42 | **2.394176419837988** | 0.00588477265722335\n",
    "6 | 45 | 2.433776088235827 | 0.02315130230634282\n",
    "6 | 48 | **2.388492100373433** | 0.00490089211441510\n",
    "6 | 49 | 2.440617942865168 | 0.03795938818655425\n",
    "6 | 50 | **2.379645842199627** | 0.00242145703304228\n",
    "**6** | **51** | **2.379316580570886** | 0.00161492136855090\n",
    "6 | 52 | 2.432784516485041 | 0.00751501142645511\n",
    "6 | 55 | 2.485440981089821 | 0.00208699822234836\n",
    "7 | 20 | 2.577312549960696 | 0.01697770346331877\n",
    "7 | 60 | 2.458714525861095 | 0.00840088147444598"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "更新params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = {\n",
    "    'boosting': 'gbdt', \n",
    "    'objective': 'multiclass',\n",
    "    'metrics' : 'multi_logloss',\n",
    "    'num_class': num_outputs,\n",
    "    'verbosity': 1,\n",
    "    'max_depth': 6,\n",
    "    'num_leaves': 51,                 #以上不再调整\n",
    "    'min_data_in_leaf' : 20,          #默认数值，备调，step3\n",
    "    'feature_fraction': 0.8,          #常用数值，备调，step4\n",
    "    'learning_rate': 0.1,             #默认数值，备调，step5\n",
    "    }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Step3: min_data_in_leaf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "min_data_in_leaf = 10\n",
      "[100]\tcv_agg's multi_logloss: 2.41101 + 0.00927644\n",
      "last mean: 2.3988022363060515\n",
      "last stdv: 0.008656683290247618\n",
      "min_data_in_leaf = 30\n",
      "[100]\tcv_agg's multi_logloss: 2.39748 + 0.0100026\n",
      "last mean: 2.383482452281396\n",
      "last stdv: 0.0011820715015853234\n"
     ]
    }
   ],
   "source": [
    "for  min_data_in_leaf in (10, 30):\n",
    "    params['min_data_in_leaf'] =  min_data_in_leaf\n",
    "    print('min_data_in_leaf = %d' % (params['min_data_in_leaf']))\n",
    "    #print(params)\n",
    "    #'''\n",
    "    results = lgb.cv(params,\n",
    "                     data_train,\n",
    "                     num_boost_round = 214,\n",
    "                     nfold = 3,\n",
    "                     shuffle = True,\n",
    "                     early_stopping_rounds = 40,\n",
    "                     verbose_eval = 100)\n",
    "    print('last mean:', results['multi_logloss-mean'][-1])\n",
    "    print('last stdv:', results['multi_logloss-stdv'][-1])\n",
    "    #'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "min_data_in_leaf = 18\n",
      "[100]\tcv_agg's multi_logloss: 2.39957 + 0.003926\n",
      "last mean: 2.3869818226693784\n",
      "last stdv: 0.0033857389768613276\n",
      "min_data_in_leaf = 19\n",
      "[100]\tcv_agg's multi_logloss: 2.46381 + 0.102631\n",
      "last mean: 2.3956323643380633\n",
      "last stdv: 0.002191965018621247\n",
      "min_data_in_leaf = 21\n",
      "[100]\tcv_agg's multi_logloss: 2.39539 + 0.00630973\n",
      "last mean: 2.3835612834535924\n",
      "last stdv: 0.004011287921746836\n",
      "min_data_in_leaf = 22\n",
      "last mean: 2.4720458242466674\n",
      "last stdv: 0.010899544962079137\n"
     ]
    }
   ],
   "source": [
    "for  min_data_in_leaf in (18, 19, 21, 22):\n",
    "    params['min_data_in_leaf'] =  min_data_in_leaf\n",
    "    print('min_data_in_leaf = %d' % (params['min_data_in_leaf']))\n",
    "    #print(params)\n",
    "    #'''\n",
    "    results = lgb.cv(params,\n",
    "                     data_train,\n",
    "                     num_boost_round = 214,\n",
    "                     nfold = 3,\n",
    "                     shuffle = True,\n",
    "                     early_stopping_rounds = 40,\n",
    "                     verbose_eval = 100)\n",
    "    print('last mean:', results['multi_logloss-mean'][-1])\n",
    "    print('last stdv:', results['multi_logloss-stdv'][-1])\n",
    "    #'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "min_data_in_leaf = 25\n",
      "[100]\tcv_agg's multi_logloss: 2.38841 + 0.00131585\n",
      "[200]\tcv_agg's multi_logloss: 2.38613 + 0.0149716\n",
      "last mean: 2.375107782694854\n",
      "last stdv: 0.004941879444657266\n",
      "min_data_in_leaf = 28\n",
      "last mean: 2.424026943571746\n",
      "last stdv: 0.001265288744226837\n"
     ]
    }
   ],
   "source": [
    "for  min_data_in_leaf in (25, 28):\n",
    "    params['min_data_in_leaf'] =  min_data_in_leaf\n",
    "    print('min_data_in_leaf = %d' % (params['min_data_in_leaf']))\n",
    "    #print(params)\n",
    "    #'''\n",
    "    results = lgb.cv(params,\n",
    "                     data_train,\n",
    "                     num_boost_round = 214,\n",
    "                     nfold = 3,\n",
    "                     shuffle = True,\n",
    "                     early_stopping_rounds = 40,\n",
    "                     verbose_eval = 100)\n",
    "    print('last mean:', results['multi_logloss-mean'][-1])\n",
    "    print('last stdv:', results['multi_logloss-stdv'][-1])\n",
    "    #'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "min_data_in_leaf = 24\n",
      "[100]\tcv_agg's multi_logloss: 2.39916 + 0.00999138\n",
      "[200]\tcv_agg's multi_logloss: 2.39929 + 0.0225062\n",
      "last mean: 2.3819529347976385\n",
      "last stdv: 0.002575471097658755\n",
      "min_data_in_leaf = 26\n",
      "[100]\tcv_agg's multi_logloss: 2.41997 + 0.0279787\n",
      "[200]\tcv_agg's multi_logloss: 2.4086 + 0.0314241\n",
      "last mean: 2.3791775270888826\n",
      "last stdv: 0.004678329123338282\n"
     ]
    }
   ],
   "source": [
    "for  min_data_in_leaf in (24, 26):\n",
    "    params['min_data_in_leaf'] =  min_data_in_leaf\n",
    "    print('min_data_in_leaf = %d' % (params['min_data_in_leaf']))\n",
    "    #print(params)\n",
    "    #'''\n",
    "    results = lgb.cv(params,\n",
    "                     data_train,\n",
    "                     num_boost_round = 214,\n",
    "                     nfold = 3,\n",
    "                     shuffle = True,\n",
    "                     early_stopping_rounds = 40,\n",
    "                     verbose_eval = 100)\n",
    "    print('last mean:', results['multi_logloss-mean'][-1])\n",
    "    print('last stdv:', results['multi_logloss-stdv'][-1])\n",
    "    #'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "min_data_in_leaf | loss-mean | loss-stdv\n",
    ":-: | :-: | :-: \n",
    "10 | 2.398802236306051 | 0.008656683290247618\n",
    "18 | 2.386981822669378 | 0.003385738976861327\n",
    "19 | 2.395632364338063 | 0.002191965018621247\n",
    "20 | **2.379316580570886** | **0.001614921368550909**\n",
    "21 | 2.383561283453592 | 0.004011287921746836\n",
    "22 | 2.472045824246667 | 0.010899544962079137\n",
    "24 | 2.381952934797638 | 0.002575471097658755\n",
    "25 | **2.375107782694854** | 0.004941879444657266\n",
    "26 | **2.379177527088882** | 0.004678329123338282\n",
    "28 | 2.424026943571746 | **0.001265288744226837**\n",
    "30 | 2.383482452281396 | **0.001182071501585323**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "更新params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = {\n",
    "    'boosting': 'gbdt', \n",
    "    'objective': 'multiclass',\n",
    "    'metrics' : 'multi_logloss',\n",
    "    'num_class': num_outputs,\n",
    "    'verbosity': 1,\n",
    "    'max_depth': 6,\n",
    "    'num_leaves': 51,\n",
    "    'min_data_in_leaf' : 25,          #以上不再调整\n",
    "    'feature_fraction': 0.8,          #常用数值，备调，step4\n",
    "    'learning_rate': 0.1,             #默认数值，备调，step5\n",
    "    }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Step4: feature_fraction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "feature_fraction | loss-mean | loss-stdv\n",
    ":-: | :-: | :-:\n",
    "0.60 | 2.385215748483773 | 0.012141673640713\n",
    "0.70 | 2.389174530713541 | 0.002666629435758\n",
    "0.76 | 2.384927671009105 | 0.003871886355262\n",
    "0.78 | 2.384927671009105 | 0.003871886355262\n",
    "0.79 | 2.375107782694854 | 0.004941879444657\n",
    "0.80 | 2.375107782694854 | 0.004941879444657\n",
    "0.81 | 2.375107782694854 | 0.004941879444657\n",
    "0.82 | 2.375107782694854 | 0.004941879444657\n",
    "0.84 | 2.402827931259069 | 0.012815010197265\n",
    "0.90 | 2.470756387332203 | 0.092283616674221"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = {\n",
    "    'boosting': 'gbdt', \n",
    "    'objective': 'multiclass',\n",
    "    'metrics' : 'multi_logloss',\n",
    "    'num_class': num_outputs,\n",
    "    'verbosity': 1,\n",
    "    'max_depth': 6,\n",
    "    'num_leaves': 51,\n",
    "    'min_data_in_leaf' : 25,\n",
    "    'feature_fraction': 0.79,\n",
    "    'learning_rate': 0.1,             #默认数值，备调，step5\n",
    "    }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Step5: learning_rate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[50]\tcv_agg's multi_logloss: 2.57307 + 0.00056697\n",
      "[100]\tcv_agg's multi_logloss: 2.51931 + 0.000638326\n",
      "[150]\tcv_agg's multi_logloss: 2.48507 + 0.000721345\n",
      "[200]\tcv_agg's multi_logloss: 2.46164 + 0.00061995\n",
      "[250]\tcv_agg's multi_logloss: 2.44478 + 0.00061603\n",
      "[300]\tcv_agg's multi_logloss: 2.43207 + 0.000663855\n",
      "[350]\tcv_agg's multi_logloss: 2.42213 + 0.000679853\n",
      "[400]\tcv_agg's multi_logloss: 2.41438 + 0.000705551\n",
      "[450]\tcv_agg's multi_logloss: 2.40821 + 0.000819548\n",
      "[500]\tcv_agg's multi_logloss: 2.40324 + 0.00086405\n",
      "[550]\tcv_agg's multi_logloss: 2.39902 + 0.000895936\n",
      "[600]\tcv_agg's multi_logloss: 2.39527 + 0.000906648\n",
      "[650]\tcv_agg's multi_logloss: 2.39197 + 0.000893011\n",
      "[700]\tcv_agg's multi_logloss: 2.38895 + 0.000808116\n",
      "[750]\tcv_agg's multi_logloss: 2.38624 + 0.00080451\n",
      "[800]\tcv_agg's multi_logloss: 2.38366 + 0.000755893\n",
      "[850]\tcv_agg's multi_logloss: 2.38128 + 0.000807969\n",
      "[900]\tcv_agg's multi_logloss: 2.37916 + 0.000790989\n",
      "[950]\tcv_agg's multi_logloss: 2.37711 + 0.00076141\n",
      "[1000]\tcv_agg's multi_logloss: 2.37528 + 0.000758945\n",
      "[1050]\tcv_agg's multi_logloss: 2.37353 + 0.000787613\n",
      "[1100]\tcv_agg's multi_logloss: 2.37189 + 0.000779996\n",
      "[1150]\tcv_agg's multi_logloss: 2.37036 + 0.000803353\n",
      "[1200]\tcv_agg's multi_logloss: 2.36895 + 0.000808919\n",
      "[1250]\tcv_agg's multi_logloss: 2.36761 + 0.0008601\n",
      "[1300]\tcv_agg's multi_logloss: 2.36642 + 0.000882368\n",
      "[1350]\tcv_agg's multi_logloss: 2.36525 + 0.000913721\n",
      "[1400]\tcv_agg's multi_logloss: 2.36409 + 0.000967512\n",
      "[1450]\tcv_agg's multi_logloss: 2.36299 + 0.000937832\n",
      "[1500]\tcv_agg's multi_logloss: 2.36199 + 0.000950958\n",
      "[1550]\tcv_agg's multi_logloss: 2.36097 + 0.000964462\n",
      "[1600]\tcv_agg's multi_logloss: 2.35998 + 0.000951326\n",
      "[1650]\tcv_agg's multi_logloss: 2.35902 + 0.000968071\n",
      "[1700]\tcv_agg's multi_logloss: 2.35816 + 0.00100527\n",
      "[1750]\tcv_agg's multi_logloss: 2.3573 + 0.00104488\n",
      "[1800]\tcv_agg's multi_logloss: 2.35649 + 0.0010251\n",
      "[1850]\tcv_agg's multi_logloss: 2.35573 + 0.00101939\n",
      "[1900]\tcv_agg's multi_logloss: 2.35499 + 0.00102803\n",
      "[1950]\tcv_agg's multi_logloss: 2.35426 + 0.00106274\n",
      "[2000]\tcv_agg's multi_logloss: 2.35359 + 0.00105556\n",
      "[2050]\tcv_agg's multi_logloss: 2.35294 + 0.00104632\n",
      "[2100]\tcv_agg's multi_logloss: 2.35233 + 0.00105411\n",
      "[2150]\tcv_agg's multi_logloss: 2.35171 + 0.00109152\n",
      "[2200]\tcv_agg's multi_logloss: 2.35109 + 0.00112038\n",
      "[2250]\tcv_agg's multi_logloss: 2.35051 + 0.00115153\n",
      "[2300]\tcv_agg's multi_logloss: 2.34992 + 0.00119521\n",
      "[2350]\tcv_agg's multi_logloss: 2.34937 + 0.00120727\n",
      "[2400]\tcv_agg's multi_logloss: 2.34886 + 0.00122717\n",
      "[2450]\tcv_agg's multi_logloss: 2.34838 + 0.00124077\n",
      "[2500]\tcv_agg's multi_logloss: 2.3479 + 0.00125343\n",
      "[2550]\tcv_agg's multi_logloss: 2.34743 + 0.00125133\n",
      "[2600]\tcv_agg's multi_logloss: 2.34701 + 0.00127166\n",
      "[2650]\tcv_agg's multi_logloss: 2.34658 + 0.00128808\n",
      "[2700]\tcv_agg's multi_logloss: 2.34615 + 0.00128581\n",
      "[2750]\tcv_agg's multi_logloss: 2.34577 + 0.00129729\n",
      "[2800]\tcv_agg's multi_logloss: 2.34536 + 0.00130474\n",
      "[2850]\tcv_agg's multi_logloss: 2.34499 + 0.00129072\n",
      "[2900]\tcv_agg's multi_logloss: 2.34464 + 0.0013008\n",
      "[2950]\tcv_agg's multi_logloss: 2.3443 + 0.00132946\n",
      "[3000]\tcv_agg's multi_logloss: 2.34392 + 0.00133851\n",
      "[3050]\tcv_agg's multi_logloss: 2.3436 + 0.00133567\n",
      "[3100]\tcv_agg's multi_logloss: 2.34327 + 0.00134082\n",
      "[3150]\tcv_agg's multi_logloss: 2.34299 + 0.00136252\n",
      "[3200]\tcv_agg's multi_logloss: 2.3427 + 0.00139557\n",
      "[3250]\tcv_agg's multi_logloss: 2.3424 + 0.00144097\n",
      "[3300]\tcv_agg's multi_logloss: 2.34213 + 0.00145782\n",
      "[3350]\tcv_agg's multi_logloss: 2.34186 + 0.00149002\n",
      "[3400]\tcv_agg's multi_logloss: 2.34161 + 0.001513\n",
      "[3450]\tcv_agg's multi_logloss: 2.34135 + 0.00150763\n",
      "[3500]\tcv_agg's multi_logloss: 2.34112 + 0.00150246\n",
      "[3550]\tcv_agg's multi_logloss: 2.3409 + 0.00149617\n",
      "[3600]\tcv_agg's multi_logloss: 2.34065 + 0.001495\n",
      "[3650]\tcv_agg's multi_logloss: 2.34043 + 0.00150887\n",
      "[3700]\tcv_agg's multi_logloss: 2.34021 + 0.00151021\n",
      "[3750]\tcv_agg's multi_logloss: 2.34 + 0.00151257\n",
      "[3800]\tcv_agg's multi_logloss: 2.33979 + 0.00150856\n",
      "[3850]\tcv_agg's multi_logloss: 2.33961 + 0.00150727\n",
      "[3900]\tcv_agg's multi_logloss: 2.33943 + 0.00150625\n",
      "[3950]\tcv_agg's multi_logloss: 2.33926 + 0.00150395\n",
      "[4000]\tcv_agg's multi_logloss: 2.33911 + 0.00148543\n",
      "[4050]\tcv_agg's multi_logloss: 2.33897 + 0.00149303\n",
      "[4100]\tcv_agg's multi_logloss: 2.3388 + 0.00150431\n",
      "[4150]\tcv_agg's multi_logloss: 2.33864 + 0.00153023\n",
      "[4200]\tcv_agg's multi_logloss: 2.3385 + 0.00154113\n",
      "[4250]\tcv_agg's multi_logloss: 2.33836 + 0.0015407\n",
      "[4300]\tcv_agg's multi_logloss: 2.33824 + 0.00154434\n",
      "[4350]\tcv_agg's multi_logloss: 2.33809 + 0.00155832\n",
      "[4400]\tcv_agg's multi_logloss: 2.33797 + 0.00157669\n",
      "[4450]\tcv_agg's multi_logloss: 2.33782 + 0.00160707\n",
      "[4500]\tcv_agg's multi_logloss: 2.33769 + 0.00162053\n",
      "[4550]\tcv_agg's multi_logloss: 2.33757 + 0.00162452\n",
      "[4600]\tcv_agg's multi_logloss: 2.33747 + 0.0016229\n",
      "[4650]\tcv_agg's multi_logloss: 2.3374 + 0.00162924\n",
      "[4700]\tcv_agg's multi_logloss: 2.33734 + 0.00162133\n",
      "[4750]\tcv_agg's multi_logloss: 2.33724 + 0.00160205\n",
      "[4800]\tcv_agg's multi_logloss: 2.33713 + 0.00159847\n",
      "[4850]\tcv_agg's multi_logloss: 2.33705 + 0.00160692\n",
      "[4900]\tcv_agg's multi_logloss: 2.33698 + 0.00161472\n",
      "[4950]\tcv_agg's multi_logloss: 2.3369 + 0.00160609\n",
      "[5000]\tcv_agg's multi_logloss: 2.33683 + 0.00160352\n",
      "[5050]\tcv_agg's multi_logloss: 2.33675 + 0.00159508\n",
      "[5100]\tcv_agg's multi_logloss: 2.33668 + 0.0015833\n",
      "[5150]\tcv_agg's multi_logloss: 2.3366 + 0.00158443\n",
      "[5200]\tcv_agg's multi_logloss: 2.33654 + 0.00159474\n",
      "[5250]\tcv_agg's multi_logloss: 2.33648 + 0.00159821\n",
      "[5300]\tcv_agg's multi_logloss: 2.33646 + 0.0016018\n",
      "[5350]\tcv_agg's multi_logloss: 2.3364 + 0.00159867\n",
      "[5400]\tcv_agg's multi_logloss: 2.33635 + 0.00159696\n",
      "[5450]\tcv_agg's multi_logloss: 2.3363 + 0.00159265\n",
      "[5500]\tcv_agg's multi_logloss: 2.33627 + 0.00159454\n",
      "[5550]\tcv_agg's multi_logloss: 2.33623 + 0.00160996\n",
      "[5600]\tcv_agg's multi_logloss: 2.33619 + 0.00160931\n",
      "[5650]\tcv_agg's multi_logloss: 2.33618 + 0.00160234\n",
      "[5700]\tcv_agg's multi_logloss: 2.33613 + 0.00158054\n",
      "[5750]\tcv_agg's multi_logloss: 2.33611 + 0.00158123\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-34-8146e141a2d9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     17\u001b[0m                  \u001b[0mshuffle\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m                  \u001b[0mearly_stopping_rounds\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m100\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 19\u001b[1;33m                  verbose_eval = 50)\n\u001b[0m\u001b[0;32m     20\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'best num_boost_round:'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresults\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'multi_logloss-mean'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     21\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'last mean:'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresults\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'multi_logloss-mean'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\.conda\\envs\\Pytorch-learn\\lib\\site-packages\\lightgbm\\engine.py\u001b[0m in \u001b[0;36mcv\u001b[1;34m(params, train_set, num_boost_round, folds, nfold, stratified, shuffle, metrics, fobj, feval, init_model, feature_name, categorical_feature, early_stopping_rounds, fpreproc, verbose_eval, show_stdv, seed, callbacks, eval_train_metric)\u001b[0m\n\u001b[0;32m    562\u001b[0m                                     \u001b[0mend_iteration\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mnum_boost_round\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    563\u001b[0m                                     evaluation_result_list=None))\n\u001b[1;32m--> 564\u001b[1;33m         \u001b[0mcvfolds\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfobj\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfobj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    565\u001b[0m         \u001b[0mres\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_agg_cv_result\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcvfolds\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0meval_valid\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfeval\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0meval_train_metric\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    566\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0m_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmean\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstd\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mres\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\.conda\\envs\\Pytorch-learn\\lib\\site-packages\\lightgbm\\engine.py\u001b[0m in \u001b[0;36mhandler_function\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    292\u001b[0m             \u001b[0mret\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    293\u001b[0m             \u001b[1;32mfor\u001b[0m \u001b[0mbooster\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mboosters\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 294\u001b[1;33m                 \u001b[0mret\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgetattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbooster\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    295\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mret\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    296\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mhandler_function\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\.conda\\envs\\Pytorch-learn\\lib\\site-packages\\lightgbm\\basic.py\u001b[0m in \u001b[0;36mupdate\u001b[1;34m(self, train_set, fobj)\u001b[0m\n\u001b[0;32m   1974\u001b[0m             _safe_call(_LIB.LGBM_BoosterUpdateOneIter(\n\u001b[0;32m   1975\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1976\u001b[1;33m                 ctypes.byref(is_finished)))\n\u001b[0m\u001b[0;32m   1977\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__is_predicted_cur_iter\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;32mFalse\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0m_\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange_\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__num_dataset\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1978\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mis_finished\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalue\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "params = {\n",
    "    'boosting': 'gbdt', \n",
    "    'objective': 'multiclass',\n",
    "    'metrics' : 'multi_logloss',\n",
    "    'num_class': num_outputs,\n",
    "    'verbosity': 1,                   #以上不再调整\n",
    "    'max_depth': 6,\n",
    "    'num_leaves': 51,                 #常用数值，备调，step2\n",
    "    'min_data_in_leaf' : 25,          #默认数值，备调，step3\n",
    "    'feature_fraction': 0.79,          #常用数值，备调，step4\n",
    "    'learning_rate': 0.01,             #默认数值，备调，step5\n",
    "    }\n",
    "results = lgb.cv(params,\n",
    "                 data_train,\n",
    "                 num_boost_round = 10000,\n",
    "                 nfold = 3,\n",
    "                 shuffle = True,\n",
    "                 early_stopping_rounds = 100,\n",
    "                 verbose_eval = 50)\n",
    "print('best num_boost_round:', len(results['multi_logloss-mean']))\n",
    "print('last mean:', results['multi_logloss-mean'][-1])\n",
    "print('last stdv:', results['multi_logloss-stdv'][-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = {\n",
    "    'boosting': 'gbdt', \n",
    "    'objective': 'multiclass',\n",
    "    'metrics' : 'multi_logloss',\n",
    "    'num_class': num_outputs,\n",
    "    'verbosity': 1,\n",
    "    'device_type':'gpu',\n",
    "    'gpu_platform_id':0,\n",
    "    'gpu_device_id':0,\n",
    "    'max_depth': 6,\n",
    "    'num_leaves': 51,\n",
    "    'min_data_in_leaf' : 25,\n",
    "    'feature_fraction': 0.79,\n",
    "    'learning_rate': 0.01,\n",
    "    }\n",
    "gbm = lgb.train(params, data_train, num_boost_round = 2000)\n",
    "gbm.save_model('../working/gbm(v2).txt')\n",
    "testResult = gbm.predict(test_features)\n",
    "sampleSubmission = pd.read_csv('../input/sf-crime/sampleSubmission.csv.zip')\n",
    "Result_pd = pd.DataFrame(testResult,\n",
    "                         index=sampleSubmission.index,\n",
    "                         columns=sampleSubmission.columns[1:])\n",
    "Result_pd.to_csv('../working/sampleSubmission(gbmv2).csv', index_label='Id')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.10 64-bit ('Pytorch-learn': conda)",
   "language": "python",
   "name": "python361064bitpytorchlearnconda080df47efea24539a61202fa66a72562"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
